/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
//	global.h	-	global definitions


#pragma once


/*-----------------------------------------------------------------------------
	check and tune compiler options
-----------------------------------------------------------------------------*/

#pragma warning (disable : 4996)	//	'function' : was declared deprecated
#pragma warning (disable : 4244)	//	'variable' : conversion from 'double' to 'float', possible loss of data
//#pragma warning (disable : 4311)	//	'variable' : pointer truncation from 'type' to 'type'
//#pragma warning (disable : 4312)	//	'variable' : conversion from 'type' to 'type' of greater size
//#pragma warning (disable : 4305)	//	'variable' : conversion from 'double' to 'float', possible loss of data*/

#ifndef _CPPRTTI
	#error	Run-time type information is disabled!
#endif

#ifndef _CPPUNWIND
	#error	Exception handling is disabled!
#endif


/*-----------------------------------------------------------------------------
	Libs
-----------------------------------------------------------------------------*/


/*-----------------------------------------------------------------------------
	General macros
-----------------------------------------------------------------------------*/

#undef DLL_EXPORT
#undef DLL_IMPORT
#if 1
	#define	DLL_EXPORT	extern "C"	__declspec(dllexport)
	#define	DLL_IMPORT	extern "C"	__declspec(dllimport)
#else
	#define	DLL_EXPORT
	#define	DLL_IMPORT
#endif

#define	SYS_CALL	__stdcall		// used for system calls


/*-----------------------------------------------------------------------------
	Headers
-----------------------------------------------------------------------------*/

#define	WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>

#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif


#define OS_MAX_PATH	MAX_PATH

#include "..\library\library.h"
#include "..\tinyxml\tinyxml.h"

#include "..\interfaces\i_console.h"
#include "..\interfaces\i_shell.h"
#include "..\interfaces\i_system.h"
#include "..\interfaces\i_filesys.h"
#include "..\interfaces\i_script.h"
#include "..\interfaces\i_input.h"
#include "..\interfaces\i_rendersystem.h"
#include "..\interfaces\i_game.h"
#include "..\interfaces\i_editor.h"
#include "..\interfaces\i_ui.h"
#include "..\interfaces\i_client.h"
#include "..\interfaces\i_server.h"
#include "..\interfaces\i_sound_system.h"
#include "..\interfaces\i_netsys.h"
#include "..\interfaces\i_core.h"
#include "..\interfaces\i_video.h"

#include "..\win32\profiler.h"
#define PROF_PROBE()			EProfProbe __prof_probe__(__FUNCTION__)

#include "win32\win_key_list.h"


/*-----------------------------------------------------------------------------
	Console macros :
-----------------------------------------------------------------------------*/

#define	LOG				con->Log
#define	LOGF			con->Logf
#define	LOG_OK			con->LogOK
#define LOG_FAILED		con->LogFailed
#define	LOG_SPLIT		con->LogSplit
#define LOG_SPLIT2(s)	con->LogSplit(s, '=');
#define	LOG_ACTION		con->LogAction
#define WARNING			con->LogWarning
	

/*-----------------------------------------------------------------------------
	OS specific dependencies :
-----------------------------------------------------------------------------*/


/*-----------------------------------------------------------------------------
	System linking stuff :
		AP	-	APplication level interfaces (console, shell, file system etc.)
		GS	-	Game System level interfaces (graphic, audio, network systems.) 
				Usually, GS is platform specific
		GS2	-	Next game System level interfaces (wrapper to GS1) 
		GI	-	Game Interfaces	(game interfaces, maybe svgame, clgame uigame)
-----------------------------------------------------------------------------*/

struct ap_t {		
		ICore			*core;
		IConsole		*con;
		ISystem			*sys;
		IFileSystem		*fs;
		IShell			*shell;
		INetworkSystem	*ns;
	};

struct gs_t {
		IRenderSystem	*rs;
		ISoundSystem	*ss;
		IInputSystem	*is;
	};
	
struct gs2_t {
		IUserInterface	*ui;
	};	

struct gi_t {
		IGame			*game;
	};
	

DLL_EXPORT	IConsole*		CreateConsole		( void );
DLL_EXPORT	ISystem*		CreateSystem		( IConsole *con );
DLL_EXPORT	IFileSystem*	CreateFileSystem	( IConsole *con );
DLL_EXPORT	IShell*			CreateShell			( IConsole *con, IFileSystem *fs );
DLL_EXPORT	IScript*		CreateScript		( IConsole *con, IFileSystem *fs, IShell *shell );
DLL_EXPORT	INetworkSystem*	CreateNetworkSystem	( IConsole *con, ISystem *sys );
DLL_EXPORT	ICore*			CreateCore			( const ap_t &ap );
DLL_EXPORT	IRenderSystem*	CreateRenderSystem	( const ap_t &ap );
DLL_EXPORT	IInputSystem*	CreateInputSystem	( const ap_t &ap );
DLL_EXPORT	ISoundSystem*	CreateSoundSystem	( const ap_t &ap );
DLL_EXPORT	IGame*			CreateGame			( const ap_t &ap, const gs_t &gs, const gs2_t &gs2 );
DLL_EXPORT	IUserInterface*	CreateUserInterface	( const ap_t &ap, const gs_t &gs );
DLL_EXPORT	IClient*		CreateClient		( const ap_t &ap, const gs_t &gs, const gi_t &gi );
DLL_EXPORT	IServer*		CreateServer		( const ap_t &ap, const gs_t &gs, const gi_t &gi );
DLL_EXPORT	IEditor*		CreateEditor		( const ap_t &ap, const gs_t &gs, const gs2_t &gs2 );
DLL_EXPORT	IVideoPlayer*	CreateVideoPlayer	( const ap_t &ap, const gs_t &gs );

DLL_EXPORT	void			DestroyConsole		( IConsole *con );
DLL_EXPORT	void			DestroySystem		( ISystem *sys );
DLL_EXPORT	void			DestroyFileSystem	( IFileSystem *fs );
DLL_EXPORT	void			DestroyShell		( IShell *shell );
DLL_EXPORT	void			DestroyScript		( IScript *script );
DLL_EXPORT	void			DestroyCore			( ICore *core );
DLL_EXPORT	void			DestroyRenderSystem	( IRenderSystem *rs );
DLL_EXPORT	void			DestroyInputSystem	( IInputSystem *is );
DLL_EXPORT	void			DestroyNetworkSystem( INetworkSystem *ns );
DLL_EXPORT	void			DestroySoundSystem	( ISoundSystem *ss );
DLL_EXPORT	void			DestroyGame			( IGame *game );
DLL_EXPORT	void			DestroyUserInterface( IUserInterface *ui );
DLL_EXPORT	void			DestroyClient		( IClient *cl );
DLL_EXPORT	void			DestroyServer		( IServer *sv );
DLL_EXPORT	void			DestroyEditor		( IEditor *ed );
DLL_EXPORT	void			DestroyVideoPlayer	( IVideoPlayer *vp );
	
	
typedef IConsole*		(*createConsole_f)			( void );
typedef ISystem*		(*createSystem_f)			( IConsole *con );
typedef	IFileSystem*	(*createFileSystem_f)		( IConsole *con );
typedef IShell*			(*createShell_f)			( IConsole *con, IFileSystem *fs );
typedef IScript*		(*createScript_f)			( IConsole *con, IFileSystem *fs, IShell *shell );
typedef INetworkSystem*	(*createNetworkSystem_f)	( IConsole *con, ISystem *sys );
typedef ICore*			(*createCore_f)				( const ap_t &ap );
typedef IRenderSystem*	(*createRenderSystem_f)		( const ap_t &ap );
typedef IInputSystem*	(*createInputSystem_f)		( const ap_t &ap );
typedef ISoundSystem*	(*createSoundSystem_f)		( const ap_t &ap );
typedef IGame*			(*createGame_f)				( const ap_t &ap, const gs_t &gs, const gs2_t &gs2 );
typedef IUserInterface*	(*createUserInterface_f)	( const ap_t &ap, const gs_t &gs );
typedef IClient*		(*createClient_f)			( const ap_t &ap, const gs_t &gs, const gi_t &gi );
typedef IServer*		(*createServer_f)			( const ap_t &ap, const gs_t &gs, const gi_t &gi );
typedef IEditor*		(*createEditor_f)			( const ap_t &ap, const gs_t &gs, const gs2_t &gs2 );
typedef IVideoPlayer*	(*createVideoPlayer_f)		( const ap_t &ap, const gs_t &gs );

typedef void			(*destroyConsole_f)			( IConsole *con );
typedef void			(*destroySystem_f)			( ISystem *sys );
typedef void			(*destroyFileSystem_f)		( IFileSystem *fs );
typedef void			(*destroyShell_f)			( IShell *shell );
typedef void			(*destroyCore_f)			( ICore *core );
typedef void			(*destroyRenderSystem_f)	( IRenderSystem *rs );
typedef void			(*destroyInputSystem_f)		( IInputSystem *is );
typedef void			(*destroyNetworkSystem_f)	( INetworkSystem *is );
typedef void			(*destroySoundSystem_f)		( ISoundSystem *is );
typedef void			(*destroyGame_f)			( IGame *game );
typedef void			(*destroyUserInterface_f)	( IUserInterface *game );
typedef void			(*destroyClient_f)			( IClient *cl );
typedef void			(*destroyDestroy_f)			( IServer *sv );
typedef void			(*destroyVideoPlayer_f)		( IVideoPlayer *ed );



	